package com.cskaoyan.mall.service;


import com.cskaoyan.mall.bean.vo.OrderGoodsStatVO;
import com.cskaoyan.mall.bean.Order;
import com.cskaoyan.mall.bean.OrderGoods;
import com.cskaoyan.mall.bean.bo.OrderListBO;
import com.cskaoyan.mall.bean.bo.OrderShipBO;
import com.cskaoyan.mall.bean.vo.GeneralItemsBean;
import com.cskaoyan.mall.bean.vo.OrderDetailVO;
import com.cskaoyan.mall.bean.vo.OrderUserInfoVO;
import com.cskaoyan.mall.mapper.OrderGoodsMapper;
import com.cskaoyan.mall.bean.vo.OrderStatVO;
import com.cskaoyan.mall.mapper.OrderMapper;
import com.cskaoyan.mall.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * Created by Zsp
 * 2021/1/9
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Override
    public Integer getOrderTotal() {
        return orderMapper.selectAll();
    }

    @Override
    public GeneralItemsBean getOrderList(OrderListBO orderListBO) {
        // 分页处理
        PageHelper.startPage(orderListBO.getPage(),orderListBO.getLimit());

        // 查询
        List<Order> orderList = orderMapper.selectOrderList(orderListBO);

        // 获取总数目
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        int total = (int) orderPageInfo.getTotal();

        return GeneralItemsBean.data(orderList,total);
    }

    @Transactional
    @Override
    public OrderDetailVO queryOrderDetail(Integer id) {
        Order order = orderMapper.selectAllById(id);
        OrderUserInfoVO orderUserInfoVO = userMapper.selectNicknameAndAvatarById(order.getUserId());
        List<OrderGoods> orderGoods = orderGoodsMapper.selectAllByOrderId(order.getId());
        return new OrderDetailVO(orderGoods,orderUserInfoVO,order);
    }

    @Override
    public int refund(Map map) {
        Integer orderId = (Integer) map.get("orderId");
        return orderMapper.updateOrderRefund(orderId);
    }

    @Override
    public int OrderShip(OrderShipBO orderShipBO) {
        return orderMapper.updateOrderShip(orderShipBO);
    }
    /**
     * 统计报表的订单统计
     * @return
     */
    @Override
    public OrderStatVO orderStat() {


        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        List<OrderStatVO.RowsBean> rows = orderMapper.selectOrderStat();

        return OrderStatVO.data(columns,rows);
    }

    /**
     * 统计报表的订单商品统计
     * @return
     */
    @Override
    public OrderGoodsStatVO goodsStat() {
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        List<OrderGoodsStatVO.RowsBean> rows = orderMapper.selectOrderGoodsStat();
        return OrderGoodsStatVO.data(columns,rows);
    }
}
